﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using Microsoft.VisualBasic;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class LabourForm : Form
    {

        MDIParent mdipar = new MDIParent();
        private string Datamanip;
        PMSContext dbcontext = new PMSContext();
        Labor dbsup = new Labor();
        FormHelpers help = new FormHelpers();
        private int rowselect;

        public LabourForm()
        {
            InitializeComponent();
        }

        private void LaborForm_Load(object sender, EventArgs e)
        {

            txtAmount.Enabled = false;
            txtTotamount.Enabled = false;
            txtNpaid.Enabled = false;

            btnPrint.Enabled = false;

            //retrieve projects
            var proj = (from p in dbcontext.Projects
                        select p).ToList();
            if (proj.Count != 0)
            {
                foreach (var prj in proj)
                {
                    cmbPName.Items.Add(prj.Project_Name);
                }
            }



            RefreshFields();
            
        }

        #region functions
        private void RefreshDB()
        {
            var sup = (from s in dbcontext.Labors
                       select s).ToList();

            LVLaborInfo.Items.Clear();
            if (sup != null)
            {

                foreach (var sqlLab in sup)
                {
                    ListViewItem lvitems = LVLaborInfo.Items.Add(sqlLab.Labourid_PK.ToString());

                    lvitems.SubItems.Add(sqlLab.Labour_Name.ToString());
                    lvitems.SubItems.Add(sqlLab.Position.ToString());
                    lvitems.SubItems.Add(sqlLab.Rate.ToString());
                    lvitems.SubItems.Add(sqlLab.NumOf_Days.ToString());
                    lvitems.SubItems.Add(sqlLab.Amount_Week.ToString());
                    lvitems.SubItems.Add(sqlLab.OT_Amount.ToString());
                    lvitems.SubItems.Add(sqlLab.Total.ToString());
                    lvitems.SubItems.Add(sqlLab.Cash_Advance.ToString());
                    lvitems.SubItems.Add(sqlLab.Net_Paid.ToString());

                    var proj = dbcontext.Projects.Find(sqlLab.Projectid_FK);
                    lvitems.SubItems.Add(proj.Project_Name);

                }
            }
            this.Refresh();
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtLName.Clear();
            cmbPName.SelectedIndex = 0;
            txtPos.Clear();
            txtRate.Clear();
            txtNdays.Clear();
            txtAmount.Clear();
            txtOTamount.Clear();

            txtTotamount.Clear();
            txtCadvance.Clear();
            txtNpaid.Clear();


            btnLDel.Enabled = false;
            btnLEdit.Enabled = false;
            btnLSave.Enabled = false;
            btnLNew.Enabled = true;
        }

        private void DisableFields()
        {
            txtLName.Enabled = false;
            //cmbPName.Enabled = false;
            txtPos.Enabled = false;
            txtRate.Enabled = false;
            txtNdays.Enabled = false;
            txtAmount.Enabled = false;
            txtOTamount.Enabled = false;
            txtTotamount.Enabled = false;
            txtCadvance.Enabled = false;
            txtNpaid.Enabled = false;

        }

        private void EnableFields()
        {
            txtLName.Enabled = true;
            cmbPName.Enabled = true;
            txtPos.Enabled = true;
            txtRate.Enabled = true;
            txtNdays.Enabled = true;
            txtOTamount.Enabled = true;
            // txtAmount.Enabled = true;
            // txtTotamount.Enabled = true;
            txtCadvance.Enabled = true;
            // txtNpaid.Enabled = true;
        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            // RefreshDB();

            btnLNew.Enabled = true;
            btnLDel.Enabled = false;
            btnLEdit.Enabled = false;
            btnLClear.Enabled = true;
            btnLSave.Enabled = false;
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVLaborInfo.Enabled = true;
        }

        private void btnLNew_Click(object sender, EventArgs e)
        {
            LVLaborInfo.Items.Clear();
            Datamanip = "Add";
            EnableFields();
            ClearFields();
            txtLName.Focus();
            btnLSave.Enabled = true;
            btnLNew.Enabled = false;
            btnPrint.Enabled = false;
            LVLaborInfo.Enabled = false;

        }

        private void btnLEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnLSave.Enabled = true;
            btnLNew.Enabled = false;
            btnLDel.Enabled = false;
            btnLEdit.Enabled = false;
            btnPrint.Enabled = false;
        }

        private void btnLSave_Click(object sender, EventArgs e)
        {
            if (txtLName.Text == "" || txtPos.Text == string.Empty || txtRate.Text == string.Empty || txtNdays.Text == string.Empty || txtAmount.Text == string.Empty || txtOTamount.Text == string.Empty || txtOTamount.Text == string.Empty || txtCadvance.Text == string.Empty || txtNpaid.Text == string.Empty)
            {
                MessageBox.Show("Please fill up the empty fields.", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (cmbPName.SelectedIndex == 0)
            {
                MessageBox.Show("Please select Project.", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //validation for special character inputs
            else if (help.SpecChar(txtLName.Text, txtLName) == false || help.SpecChar(txtRate.Text, txtRate) == false || help.SpecChar(txtNdays.Text, txtNdays) == false || help.SpecChar(txtAmount.Text, txtAmount) == false || help.SpecChar(txtOTamount.Text, txtOTamount) == false || help.SpecChar(txtTotamount.Text, txtTotamount) == false || help.SpecChar(txtCadvance.Text, txtCadvance) == false || help.SpecChar(txtNpaid.Text, txtNpaid) == false)
            {

                MessageBox.Show("Special Characters are not allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {

                if (Datamanip == "Add")
                {
                    var proj = (from p in dbcontext.Projects
                                where p.Project_Name == cmbPName.Text
                                select p).SingleOrDefault();

                    var add = (from c in dbcontext.Labors
                               where c.Labour_Name == txtLName.Text && c.Projectid_FK == proj.Projectid_PK
                               select c).ToList();

                    if (add.Count == 0)
                    {

                        dbsup.Projectid_FK = proj.Projectid_PK;
                        dbsup.Labour_Name = txtLName.Text;
                        dbsup.Position = txtPos.Text;
                        dbsup.Rate =  txtRate.Text;
                        dbsup.NumOf_Days =  txtNdays.Text;
                        dbsup.Amount_Week =  txtAmount.Text;
                        dbsup.OT_Amount =  txtOTamount.Text;
                        dbsup.Total =  txtTotamount.Text;
                        dbsup.Cash_Advance =  txtCadvance.Text;
                        dbsup.Net_Paid =  txtNpaid.Text;

                        dbcontext.Entry(dbsup).State = EntityState.Added;
                        dbcontext.SaveChanges();
                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();

                    }
                    else
                    {
                        foreach (var c in add)
                        {
                            if (c.Labour_Name == txtLName.Text)
                            {
                                DialogResult YN = MessageBox.Show("You've entered an existing client, do you want to replace it?", "Data Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                if (YN == DialogResult.Yes)
                                {
                                    var replace = dbcontext.Labors.Find(c.Labourid_PK);
                                    replace.Projectid_FK = proj.Projectid_PK;
                                    replace.Labour_Name = txtLName.Text;
                                    replace.Position = txtPos.Text;
                                    replace.Rate =  txtRate.Text;
                                    replace.NumOf_Days =  txtNdays.Text;
                                    replace.Amount_Week =  txtAmount.Text;
                                    replace.OT_Amount =  txtOTamount.Text;
                                    replace.Total =  txtTotamount.Text;
                                    replace.Cash_Advance =  txtCadvance.Text;
                                    replace.Net_Paid =  txtNpaid.Text;

                                    dbcontext.Entry(replace).State = EntityState.Modified;
                                    dbcontext.SaveChanges();

                                    MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    RefreshFields();
                                }
                            }
                            else
                            {
                                dbsup.Labour_Name = txtLName.Text;
                                dbsup.Projectid_FK = proj.Projectid_PK;
                                dbsup.Position = txtPos.Text;
                                dbsup.Rate =  txtRate.Text;
                                dbsup.NumOf_Days =  txtNdays.Text;
                                dbsup.Amount_Week =  txtAmount.Text;
                                dbsup.OT_Amount =  txtOTamount.Text;
                                dbsup.Total =  txtTotamount.Text;
                                dbsup.Cash_Advance =  txtCadvance.Text;
                                dbsup.Net_Paid =  txtNpaid.Text;

                                dbcontext.Entry(dbsup).State = EntityState.Added;
                                dbcontext.SaveChanges();

                                MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                RefreshFields();

                            }
                        }
                    }
                    LVLaborInfo.Enabled = true;
                }

                else if (Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        var proj = (from p in dbcontext.Projects
                                    where p.Project_Name == cmbPName.SelectedItem
                                    select p).SingleOrDefault();

                        var replace = dbcontext.Labors.Find(Convert.ToInt32(txtID.Text));
                        replace.Labour_Name = txtLName.Text;
                        replace.Projectid_FK = proj.Projectid_PK;
                        replace.Position = txtPos.Text;
                        replace.Rate =  txtRate.Text;
                        replace.NumOf_Days =  txtNdays.Text;
                        replace.Amount_Week =  txtAmount.Text;
                        replace.OT_Amount =  txtTotamount.Text;
                        replace.Total =  txtTotamount.Text;
                        replace.Cash_Advance =  txtCadvance.Text;
                        replace.Net_Paid =  txtNpaid.Text;


                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();

                    }
                }
            }
        }

        private void LVLaborInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            rowselect = LVLaborInfo.FocusedItem.Index;

            txtID.Text = LVLaborInfo.Items[rowselect].SubItems[0].Text;
            txtLName.Text = LVLaborInfo.Items[rowselect].SubItems[1].Text;
            txtPos.Text = LVLaborInfo.Items[rowselect].SubItems[2].Text;
            txtRate.Text = LVLaborInfo.Items[rowselect].SubItems[3].Text;
            txtNdays.Text = LVLaborInfo.Items[rowselect].SubItems[4].Text;
            txtAmount.Text = LVLaborInfo.Items[rowselect].SubItems[5].Text;
            txtOTamount.Text = LVLaborInfo.Items[rowselect].SubItems[6].Text;
            txtTotamount.Text = LVLaborInfo.Items[rowselect].SubItems[7].Text;
            txtCadvance.Text = LVLaborInfo.Items[rowselect].SubItems[8].Text;
            txtNpaid.Text = LVLaborInfo.Items[rowselect].SubItems[9].Text;
            cmbPName.SelectedItem = LVLaborInfo.Items[rowselect].SubItems[10].Text;

            help.getPname(cmbPName.SelectedItem.ToString());

            btnLDel.Enabled = true;
            btnLEdit.Enabled = true;
            btnLSave.Enabled = false;
            btnLNew.Enabled = true;
            btnPrint.Enabled = true;
        }

        private void btnLDel_Click(object sender, EventArgs e)
        {

            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Labors.Find(Convert.ToInt32(txtID.Text));
                if (del != null)
                {
                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }


            }
            RefreshFields();

        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            help.GetPrintStat("nALL");

            LaborRptForm LRForm = new LaborRptForm();

            LRForm.Show();
        }

        private void btnPrintAll_Click(object sender, EventArgs e)
        {
            help.GetPrintStat("ALL");

            LaborRptForm LRForm = new LaborRptForm();

            LRForm.Show();
        }





        private void txtRate_TextChanged(object sender, EventArgs e)
        {
            if (txtRate.Text == "")
            {
                if (txtNdays.Text != "")
                {
                    txtAmount.Text = txtNdays.Text;
                    return;
                }
                else
                {
                    txtAmount.Text = txtRate.Text;
                    return;
                }
            }


            if ((Information.IsNumeric(txtRate.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtRate.Clear();

                //    TotalCostFunc();
                //}
            }
            else if (txtRate.Text == "")
            {

                var x = double.Parse(txtNdays.Text) * 1;
                txtAmount.Text = Convert.ToString(x);


            }
            else if (txtNdays.Text == "")
            {
                var x = double.Parse(txtRate.Text) * 1;
                txtAmount.Text = Convert.ToString(x);

            }


            else
            {


                var x = double.Parse(txtRate.Text) * double.Parse(txtNdays.Text);
                txtAmount.Text = Convert.ToString(x);


            }


            //##########################################################################



            if (txtNdays.Text != "" && txtOTamount.Text != "" && txtCadvance.Text != "")
            {
                var x = double.Parse(txtAmount.Text) + double.Parse(txtOTamount.Text);
                txtTotamount.Text = Convert.ToString(x);

                var y = double.Parse(txtTotamount.Text) - double.Parse(txtCadvance.Text);
                txtNpaid.Text = Convert.ToString(y);
            }

        }

        private void txtNdays_TextChanged(object sender, EventArgs e)
        {

            if (txtNdays.Text == "")
            {

                if (txtRate.Text != "")
                {
                    txtAmount.Text = txtRate.Text;
                    return;
                }
                else
                {

                    txtAmount.Text = txtNdays.Text;
                    return;
                }
            }
            if ((Information.IsNumeric(txtNdays.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtNdays.Clear();

                //    TotalCostFunc();
                //}
            }
            else if (txtRate.Text == "")
            {

                var x = double.Parse(txtNdays.Text) * 1;
                txtAmount.Text = Convert.ToString(x);


            }
            else if (txtNdays.Text == "")
            {
                var x = double.Parse(txtRate.Text) * 1;
                txtAmount.Text = Convert.ToString(x);

            }


            else
            {


                var x = double.Parse(txtRate.Text) * double.Parse(txtNdays.Text);
                txtAmount.Text = Convert.ToString(x);



            }


            //######################################################################################################

            if (txtRate.Text != "" && txtOTamount.Text != "" && txtCadvance.Text != "")
            {
                var x = double.Parse(txtAmount.Text) + double.Parse(txtOTamount.Text);
                txtTotamount.Text = Convert.ToString(x);

                var y = double.Parse(txtTotamount.Text) - double.Parse(txtCadvance.Text);
                txtNpaid.Text = Convert.ToString(y);
            }



        }




        private void txtAmount_TextChanged(object sender, EventArgs e)
        {


            if (txtAmount.Text == "")
            {

                if (txtOTamount.Text != "")
                {
                    txtTotamount.Text = txtTotamount.Text;
                    return;
                }
                else
                {

                    txtTotamount.Text = txtAmount.Text;
                    return;
                }


            }



            if ((Information.IsNumeric(txtAmount.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtAmount.Clear();


            }
            else if (txtAmount.Text == "")
            {

                var x = double.Parse(txtOTamount.Text) + 0;
                txtTotamount.Text = Convert.ToString(x);


            }
            else if (txtOTamount.Text == "")
            {
                var x = double.Parse(txtAmount.Text) + 0;
                txtTotamount.Text = Convert.ToString(x);

            }


            else
            {


                var x = double.Parse(txtAmount.Text) + double.Parse(txtTotamount.Text);
                txtTotamount.Text = Convert.ToString(x);



            }

        }

        private void txtOTamount_TextChanged(object sender, EventArgs e)
        {

            if (txtOTamount.Text == "")
            {

                if (txtAmount.Text != "")
                {
                    txtTotamount.Text = txtAmount.Text;
                    return;
                }
                else
                {

                    txtTotamount.Text = txtOTamount.Text;
                    return;
                }


            }



            if ((Information.IsNumeric(txtOTamount.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtOTamount.Clear();


            }
            else if (txtAmount.Text == "")
            {

                var x = double.Parse(txtOTamount.Text) + 0;
                txtTotamount.Text = Convert.ToString(x);


            }
            else if (txtOTamount.Text == "")
            {
                var x = double.Parse(txtAmount.Text) + 0;
                txtTotamount.Text = Convert.ToString(x);

            }


            else
            {


                var x = double.Parse(txtAmount.Text) + double.Parse(txtOTamount.Text);
                txtTotamount.Text = Convert.ToString(x);



            }

        }

        private void txtTotamount_TextChanged(object sender, EventArgs e)
        {


            if (txtTotamount.Text == "")
            {

                if (txtCadvance.Text != "")
                {
                    txtNpaid.Text = txtCadvance.Text;
                    return;
                }
                else
                {

                    txtNpaid.Text = txtTotamount.Text;
                    return;
                }


            }



            if ((Information.IsNumeric(txtTotamount.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtTotamount.Clear();


            }
            else if (txtTotamount.Text == "")
            {

                txtNpaid.Text = txtTotamount.Text;


            }
            else if (txtCadvance.Text == "")
            {
                var x = double.Parse(txtTotamount.Text) - 0;
                txtNpaid.Text = Convert.ToString(x);

            }


            else
            {


                var x = double.Parse(txtTotamount.Text) - double.Parse(txtCadvance.Text);
                txtNpaid.Text = Convert.ToString(x);



            }




        }

        private void txtCadvance_TextChanged(object sender, EventArgs e)
        {

            if (txtCadvance.Text == "")
            {

                if (txtTotamount.Text != "")
                {
                    txtNpaid.Text = txtTotamount.Text;
                    return;
                }
                else
                {

                    txtNpaid.Text = txtCadvance.Text;
                    return;
                }


            }



            if ((Information.IsNumeric(txtCadvance.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtCadvance.Clear();


            }
            else if (txtTotamount.Text == "")
            {

                txtNpaid.Text = txtTotamount.Text;


            }
            else if (txtCadvance.Text == "")
            {
                var x = double.Parse(txtTotamount.Text) - 0;
                txtNpaid.Text = Convert.ToString(x);

            }


            else
            {


                var x = double.Parse(txtTotamount.Text) - double.Parse(txtCadvance.Text);
                txtNpaid.Text = Convert.ToString(x);



            }





        }

        private void cmbPName_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (txtLName.Enabled == true)
            {
                return;
            }
            else if (cmbPName.Text == "--Select Project--")
            {
                LVLaborInfo.Items.Clear();
                return;

            }
            else
            {
                var cf = (from x in dbcontext.Projects
                          where x.Project_Name == cmbPName.Text
                          select x).SingleOrDefault();


                var sup = (from m in dbcontext.Labors
                           where m.Projectid_FK == cf.Projectid_PK
                           select m).ToList();



                //var sup = (from s in dbcontext.Labors
                //           select s).ToList();

                LVLaborInfo.Items.Clear();
                if (sup != null)
                {

                    foreach (var sqlLab in sup)
                    {
                        ListViewItem lvitems = LVLaborInfo.Items.Add(sqlLab.Labourid_PK.ToString());

                        lvitems.SubItems.Add(sqlLab.Labour_Name.ToString());
                        lvitems.SubItems.Add(sqlLab.Position.ToString());
                        lvitems.SubItems.Add(sqlLab.Rate.ToString());
                        lvitems.SubItems.Add(sqlLab.NumOf_Days.ToString());
                        lvitems.SubItems.Add(sqlLab.Amount_Week.ToString());
                        lvitems.SubItems.Add(sqlLab.OT_Amount.ToString());
                        lvitems.SubItems.Add(sqlLab.Total.ToString());
                        lvitems.SubItems.Add(sqlLab.Cash_Advance.ToString());
                        lvitems.SubItems.Add(sqlLab.Net_Paid.ToString());

                        var proj = dbcontext.Projects.Find(sqlLab.Projectid_FK);
                        lvitems.SubItems.Add(proj.Project_Name);

                    }
                }
            }
        }

        private void cmbPName_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cmbPName.SelectedIndex == 0)
            {
                LVLaborInfo.Items.Clear();
                return;
            }
            else
            {
                var cf = (from x in dbcontext.Projects
                          where x.Project_Name == cmbPName.Text
                          select x).SingleOrDefault();
                if (cf != null)
                {
                    LVLaborInfo.Items.Clear();

                    var lab = (from ct in dbcontext.Labors
                               where ct.Projectid_FK == cf.Projectid_PK
                               select ct).ToList();
                    if (lab.Count() != 0)
                    {
                        foreach (var itms in lab)
                        {
                            ListViewItem lvitems = LVLaborInfo.Items.Add(itms.Labourid_PK.ToString());
                            lvitems.SubItems.Add(itms.Labour_Name.ToString());
                            lvitems.SubItems.Add(itms.Position.ToString());
                            lvitems.SubItems.Add(itms.Rate.ToString());
                            lvitems.SubItems.Add(itms.NumOf_Days.ToString());
                            lvitems.SubItems.Add(itms.Amount_Week.ToString());
                            lvitems.SubItems.Add(itms.OT_Amount.ToString());
                            lvitems.SubItems.Add(itms.Total.ToString());
                            lvitems.SubItems.Add(itms.Cash_Advance.ToString());
                            lvitems.SubItems.Add(itms.Net_Paid.ToString());;
                        }
                    }
                }
             }
         }

    }
}
    

